#pragma once

#include <string>
#include <vector>
#include <fstream>
#include <atomic>
#include <sys/stat.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        TimeUtil() = default;
        ~TimeUtil() = default;

        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }
        static std::string GetTimeMillisecond()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };   


    const std::string temp_path = "./temp/";
    class PathUtil
    {
    public:
        PathUtil() = default;
        ~PathUtil() = default;
        
        static std::string AddSuffix(const std::string& file_name, const std::string& suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;
            return std::move(path_name);
        }

        static std::string Src(const std::string& file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string& file_name)
        {
            return AddSuffix(file_name, ".exe");            
        }
        static std::string CompileError(const std::string& file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        static std::string Stdin(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };  


    class FileUtil
    {
    public:
        FileUtil() = default;
        ~FileUtil() = default;

        static bool IsFileExists(const std::string& path_name)
        {
            struct stat st;
            if(stat(path_name.c_str(), &st) == 0)
                return true;
            return false;
        }

        static std::string UniqueFileName()
        {
            // 通过毫秒级时间戳 + 原子性递增的唯一值来保证文件名的唯一性           
            static std::atomic_uint64_t id(0);
            ++id;
            std::string file_name = TimeUtil::GetTimeMillisecond();
            file_name += "_";
            file_name += std::to_string(id);
            return std::move(file_name);
        }

        static bool WriteFile(const std::string& target_file, const std::string& content)
        {
            std::ofstream out(target_file);
            if(!out.is_open()) return false;
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string& target_file, std::string* content, bool keep = false)
        {
            (*content).clear();

            std::ifstream in(target_file);
            if(!in.is_open()) return false;

            std::string line;
            while (getline(in, line))
            {
                (*content) += line;
                (*content) += keep ? "\n" : "";
            }
            in.close();
            return true;
        }
    };


    class StringUtil
    {
    public:
        StringUtil() = default;
        ~StringUtil() = default;

        static void SplitString(const std::string& str, std::vector<std::string>* target, const std::string& sep)
        {
            // boost split
            // is_any_of(sep): 遇到分隔字符串中的每个字符都做分隔
            // token_compress_on: 当遇到连续分隔符时，压缩为一个处理(即不分隔出来一个空串)
            boost::split(*target, str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };  
} 